﻿var print_tab = {
    isWindow: "Y",
    ContentId: "",
    Content: "This is Defualt Content",
    IsTable: "N",
    winStyle: "width=800,height=800",
    winTitle: "DefualtWindow",
    repeatCol: "Y",
    repeatType: "first",
    repeatColCount: 4,
    totColDisplayInOnePage: 10,
    replaceInputElement:false,
    print: function () {
        if (this.isWindow.toUpperCase() == "Y") {
            var _strContent = "";
            var _conele = document.getElementById(this.ContentId);
            alert(this.ContentId);
            if (_conele) {
                if (this.IsTable.toUpperCase() == "Y") {
                    var manual = 0;
                    if (parseInt(this.totColDisplayInOnePage) == -1) {
                        manual = -1;
                    }
                    var row_length = _conele.rows.length;
                    var lastrecord = 1;
                    if (parseInt(row_length) > 0) {
                        var collength = Math.round(_conele.rows[0].cells.length / this.totColDisplayInOnePage);
                        lastrecord = parseInt(Math.round(_conele.rows[0].cells.length % this.totColDisplayInOnePage)) - parseInt(this.repeatColCount);
                    }
                    if (parseInt(lastrecord) <= 0) {
                        collength = parseInt(collength) - 1;
                    }
                    if (parseInt(this.totColDisplayInOnePage) == -1) {
                        collength = 1;
                    }
                    for (var k = 0; k < collength; k++) {
                        if (k > 0) {
                            _strContent += "<table " + this.getAttr_coll(_conele, 'class', 'page_break,fnt') + ">";
                        }
                        else {
                            _strContent += "<table " + this.getAttr_coll(_conele, 'class', 'fnt') + ">";
                        }
                        for (var i = 0; i < _conele.rows.length; i++) {
                            _strContent += "<tr>";
                            /*Column count*/
                            if (this.repeatCol == "Y") {
                                if (k > 0) {
                                    if (this.repeatType.toLowerCase() == "first") {
                                        for (var colRept = 0; colRept < this.repeatColCount; colRept++) {
                                            if (_conele.rows[i].cells[colRept] != undefined) {
                                                if (i == 0) {
                                                    _strContent += "<th " + this.getAttr_coll(_conele.rows[i].cells[colRept], '', '') + ">" + _conele.rows[i].cells[colRept].innerHTML + "</th>";
                                                }
                                                else {
                                                    _strContent += "<td " + this.getAttr_coll(_conele.rows[i].cells[colRept], '', '') + ">" + _conele.rows[i].cells[colRept].innerHTML + "</td>";
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                            var first = 0;
                            if (k > 0) {
                                first = this.repeatColCount;
                            }
                            if (parseInt(manual) == -1) {
                                this.totColDisplayInOnePage = _conele.rows[i].cells.length;
                            }
                            for (j = (parseInt(k * this.totColDisplayInOnePage) + parseInt(first)) ; j < (parseInt((k + 1) * this.totColDisplayInOnePage) + parseInt(this.repeatColCount)) ; j++) {
                                if (_conele.rows[i].cells[j] != undefined) {
                                    var attr = "";
                                    if (i == 0) {
                                        _strContent += "<th " + this.getAttr_coll(_conele.rows[i].cells[j], '', '') + ">" + _conele.rows[i].cells[j].innerHTML+ "</th>";
                                    }
                                    else {
                                        _strContent += "<td " + this.getAttr_coll(_conele.rows[i].cells[j], '', '') + ">" + _conele.rows[i].cells[j].innerHTML + "</td>";
                                    }
                                    //alert("j===" + j + "innser htnl=" + _conele.rows[i].cells[j].innerHTML);
                                }
                            }
                            _strContent += "</tr>"
                        }
                        _strContent += "</table></br>";
                    }
                }
                else {
                    _strContent += _conele.innerHTML;
                }
            }
            else {
                _strContent += this.Content;
            }
            var style = ""
            var _content = "<html><head><link rel='Stylesheet' type='text/css' href='StyleSheet.css'/><style type = 'text/css'>@media print,screen{table{font-size:12pt !important;page-break-before:always}} thead {display:table-header-group;} tfoot{display:table-footer-group;}</style><title>Print Preview::" + this.winTitle + "</title></head><body>" + _strContent + "</body></html>"
            var win = window.open("", "Preview", "width = 800, height = 800");
            alert(_content);
            win.document.write(_content);
            win.print();
        }
        else {
            window.print();
        }
    },
    getAttr_coll: function (ele, addatrr_name, addatrr_value) {
        var attr_val = "";
        if (ele) {
            for (var attr = 0; attr < ele.attributes.length; attr++) {
                if (addatrr_name != "") {
                    if (ele.attributes[attr].name.toUpperCase() == addatrr_name.toUpperCase()) {
                        attr_val += ele.attributes[attr].name + "='" + addatrr_value + "," + ele.attributes[attr].value + "' "
                    }
                    else {
                        attr_val += ele.attributes[attr].name + "='" + ele.attributes[attr].value + "' "
                    }
                }
                else {
                    attr_val += ele.attributes[attr].name + "='" + ele.attributes[attr].value + "' "
                }
            }
        }
        return attr_val;
    },
    get_value: function (cell, replace) {
        var return_value=""; 
        if (replace=="Y") {
            var ele = cell.getElementsByTagName('input');
            if (ele) {
                for (var g = 0; g < ele.length; g++) {
                    if (ele[g]) {
                        if (ele[g].type == "checkbox" || ele[g].type == "radio") {
                            if (ele[g].checked) {
                                return_value += ele[g].name;
                            }
                        } else if (ele[g].type == "password" || ele[g].type == "textbox") {
                            return_value += ele[j].value + " ";
                        } else {
                            return_value = cell.innerHTML;
                        }
                    }
                }
            }
            else {
                return_value += cell.innerHTML;
            }
        } else {
            return_value = cell.innerHTML;
        }
        return return_value;
    }
}